<?php


namespace app\system\service;

class IndexService
{
	public static function get_index_data($options)
	{
		list($today_start, $today_end) = \think\helper\Time::today();
		list($yesterday_start, $yesterday_end) = \think\helper\Time::yesterday();
		$Ymd = date("Ymd");
		$data["overview"] = ["appTotal" => \think\Db::name("app")->count(), "agentTotal" => \think\Db::name("app_agent")->count(), "apiTotal" => \think\Db::name("app_api")->count()];
		$apps = \think\Db::name("app")->order("id", "asc")->select();
		$application = [];
		foreach ($apps as $app) {
			if ($app["type"] === 0) {
				$userTotal = \think\Db::name("app_card_user")->where(["appid" => $app["id"]])->count();
			} elseif ($app["type"] === 1) {
				$userTotal = \think\Db::name("app_member_user")->where(["appid" => $app["id"]])->count();
			} elseif ($app["type"] === 2) {
				$userTotal = \think\Db::name("app_auth_user")->where(["appid" => $app["id"]])->count();
			}
			$application[] = ["id" => $app["id"], "name" => $app["name"], "avatar" => $app["avatar"], "type" => $app["type"], "addtime" => $app["addtime"], "statisticsData" => ["agentAuthorizeTotal" => \think\Db::name("app_agent_binding")->where("appid", $app["id"])->count(), "userTotal" => $userTotal, "variableTotal" => \think\Db::name("app_variable")->where("appid", $app["id"])->count(), "versionTotal" => \think\Db::name("app_version")->where("appid", $app["id"])->count()]];
		}
		$data["application"] = $application;
		$data["proportion"] = ["application" => ["card" => \think\Db::name("app")->where("type", 0)->count(), "member" => \think\Db::name("app")->where("type", 1)->count(), "auth" => \think\Db::name("app")->where("type", 2)->count()]];
		$data["chart"] = ["hour24" => ["dates" => self::getHourDate(false), "names" => self::getHourDate(true), "list" => []]];
		return $data;
	}
	private static function getHourDate($type)
	{
		$dates = [];
		$names = [];
		for ($i = 0; $i <= 23; $i++) {
			$hour = $i >= 10 ? $i : "0" . $i;
			$dates[] = $i;
			$names[] = $i . "时~" . ($i + 1) . "时";
		}
		if ($type == true) {
			return $names;
		} else {
			return $dates;
		}
	}
}